package demo.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import demo.entity.User;
import demo.entity.Weibo;
import demo.service.WeiboService;
import demo.util.ContentFilter;
import demo.util.FormatDate;
import demo.util.Html2Text;

@SuppressWarnings("serial")
public class WeiboAction extends BaseAction {

	private Weibo weibo;
	private WeiboService weiboService;
	private String result;
	private String search;
	private String type;
	
	public String list() throws Exception {
System.out.println("in WeiboAction list()...");
		User user = (User) getSession().get("user");
		List<Object[]> list = weiboService.queryWeiboByUserId(user.getId(), type);
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for (Object[] objects : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", objects[0]);
			map.put("content", ContentFilter.getFiltratedContent((String) objects[1]));
			map.put("time", FormatDate.getFormattedDate((Date) objects[2]));
			map.put("picture", objects[3]);
			map.put("client", objects[4]);
			map.put("userid", objects[5]);
			map.put("forwardid", objects[6]);
			map.put("nick", objects[7]);
			map.put("avatar", objects[8]);
			if(Long.valueOf(objects[6].toString()) != -1) {
				Map<String, Object> forwardinfo = getForwardInfo(Long.valueOf(objects[6].toString()));
				map.put("allComments", forwardinfo.get("allComments"));
				Object[] primeWeibo = (Object[]) forwardinfo.get("primeWeibo");
				if(primeWeibo != null) {
					map.put("sourceWeibo", 1);
					map.put("fweiboid", primeWeibo[0]);
					map.put("fcontent", primeWeibo[1]);
					map.put("ftime", primeWeibo[2]);
					map.put("fuserid", primeWeibo[3]);
					map.put("fnick", primeWeibo[4]);
					map.put("favatar", primeWeibo[5]);
					map.put("fclient", primeWeibo[6]);
					map.put("fpicture", primeWeibo[7]);
				} else {
					map.put("sourceWeibo", 0);
				}
			}
			maps.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(maps);
		this.result = jsonArray.toString();
System.out.println(result);
		return SUCCESS;
	}
	
	public String publish() throws Exception {
		User user = (User) getSession().get("user");
		weibo.setTime(new Date());
		weibo.setUserid(user.getId());
		weibo.setContent(Html2Text.getText(weibo.getContent()));
		Long id = weiboService.save(weibo);
		List<Object[]> list = weiboService.queryWeiboByWeiboId(id);
		Object[] objects = list.get(0);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", objects[0]);
		map.put("content", ContentFilter.getFiltratedContent((String) objects[1]));
		map.put("time", FormatDate.getFormattedDate((Date) objects[2]));
		map.put("picture", objects[3]);
		map.put("client", objects[4]);
		map.put("userid", objects[5]);
		map.put("forwardid", objects[6]);
		map.put("nick", objects[7]);
		map.put("avatar", objects[8]);
		if(Long.valueOf(objects[6].toString()) != -1) {
			Map<String, Object> forwardinfo = getForwardInfo(Long.valueOf(objects[6].toString()));
			map.put("allComments", forwardinfo.get("allComments"));
			Object[] primeWeibo = (Object[]) forwardinfo.get("primeWeibo");
			if(primeWeibo != null) {
				map.put("sourceWeibo", 1);
				map.put("fweiboid", primeWeibo[0]);
				map.put("fcontent", primeWeibo[1]);
				map.put("ftime", primeWeibo[2]);
				map.put("fuserid", primeWeibo[3]);
				map.put("fnick", primeWeibo[4]);
				map.put("favatar", primeWeibo[5]);
				map.put("fclient", primeWeibo[6]);
				map.put("fpicture", primeWeibo[7]);
			} else {
				map.put("sourceWeibo", 0);
			}
		}
		JSONObject jsonObject = JSONObject.fromObject(map);
		this.result = jsonObject.toString();
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		weiboService.delete(weibo.getId());
		return null;
	}
	
	private Map<String, Object> getForwardInfo(long weiboid) {
		List<Object[]> list = weiboService.queryForwardByWeiboId(weiboid);
		Map<String, Object> map = new HashMap<String, Object>();
		String allComments = "";
		Object[] primeWeibo = new Object[8];
		boolean flag = true;
		for (Object[] objects : list) {
			System.out.println(JSONArray.fromObject(objects));
			long forwardid = Long.valueOf(objects[5].toString());
			if(forwardid != -1) {//从当前转发的微博开始追溯原始微博，拼接之前转发nick及comment
				allComments += "//@"+objects[7]+"："+objects[1];
			}
			if(forwardid == -1) {//到达了原始微博，取得原始微博信息
				flag = false;//修改标志位
				primeWeibo[0] = Long.valueOf(objects[0].toString());
				primeWeibo[1] = ContentFilter.getFiltratedContent((String) objects[1]);
				primeWeibo[2] = FormatDate.getFormattedDate((Date) objects[2]);
				primeWeibo[3] = objects[6];
				primeWeibo[4] = ContentFilter.getFiltratedContent("@"+objects[7]);
				primeWeibo[5] = objects[8];
				primeWeibo[6] = objects[4];
				primeWeibo[7] = objects[3];
			}
		}
		map.put("allComments", ContentFilter.getFiltratedContent(allComments));
		if(flag) {
			primeWeibo = null;
		}
		map.put("primeWeibo", primeWeibo);
		return map;
	}
	
	public String search() throws Exception {
		List<Object[]> list = weiboService.queryWeiboByWeiboContent(search);
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for (Object[] objects : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", objects[0]);
			map.put("content", ContentFilter.getFiltratedContent((String) objects[1], search));
			map.put("time", FormatDate.getFormattedDate((Date) objects[2]));
			map.put("picture", objects[3]);
			map.put("client", objects[4]);
			map.put("userid", objects[5]);
			map.put("forwardid", objects[6]);
			map.put("nick", objects[7]);
			map.put("avatar", objects[8]);
			if(Long.valueOf(objects[6].toString()) != -1) {
				Map<String, Object> forwardinfo = getForwardInfo(Long.valueOf(objects[6].toString()));
				map.put("allComments", forwardinfo.get("allComments"));
				Object[] primeWeibo = (Object[]) forwardinfo.get("primeWeibo");
				if(primeWeibo != null) {
					map.put("sourceWeibo", 1);
					map.put("fweiboid", primeWeibo[0]);
					map.put("fcontent", primeWeibo[1]);
					map.put("ftime", primeWeibo[2]);
					map.put("fuserid", primeWeibo[3]);
					map.put("fnick", primeWeibo[4]);
					map.put("favatar", primeWeibo[5]);
					map.put("fclient", primeWeibo[6]);
					map.put("fpicture", primeWeibo[7]);
				} else {
					map.put("sourceWeibo", 0);
				}
			}
			maps.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(maps);
		this.result = jsonArray.toString();
		return SUCCESS;
	}
	
	public String method() throws Exception {
		//	TODO
		return SUCCESS;
	}
	
	
	//	setters and getters
	
	public Weibo getWeibo() {
		return weibo;
	}

	public void setWeibo(Weibo weibo) {
		this.weibo = weibo;
	}

	public void setWeiboService(WeiboService weiboService) {
		this.weiboService = weiboService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
